package com.ruoyi.project.wms.outgoods.service.impl;

import java.util.List;

import com.ruoyi.project.wms.logistics.ordermanager.domain.WmsOrder;
import com.ruoyi.project.wms.logistics.ordermanager.mapper.WmsOrderMapper;
import com.ruoyi.project.wms.outgoods.domain.WmsOutGoods;
import com.ruoyi.project.wms.outgoods.mapper.WmsOutGoodsMapper;
import com.ruoyi.project.wms.outgoods.service.IWmsOutGoodsService;
import com.ruoyi.project.wms.store.domain.WmsStore;
import com.ruoyi.project.wms.store.mapper.WmsStoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.text.Convert;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-10-03
 */
@Service
public class WmsOutGoodsServiceImpl implements IWmsOutGoodsService
{
    @Resource
    private WmsOutGoodsMapper wmsOutGoodsMapper;
    @Resource
    private WmsOrderMapper wmsOrderMapper;
    @Resource
    private WmsStoreMapper wmsStoreMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param ogid 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public WmsOutGoods selectWmsOutGoodsById(Long ogid)
    {
        return wmsOutGoodsMapper.selectWmsOutGoodsById(ogid);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param wmsOutGoods 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<WmsOutGoods> selectWmsOutGoodsList(WmsOutGoods wmsOutGoods)
    {
        wmsOutGoods.setOgstatus("待出库");
        return wmsOutGoodsMapper.selectWmsOutGoodsList(wmsOutGoods);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param wmsOutGoods 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertWmsOutGoods(WmsOutGoods wmsOutGoods)
    {
        return wmsOutGoodsMapper.insertWmsOutGoods(wmsOutGoods);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param wmsOutGoods 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateWmsOutGoods(WmsOutGoods wmsOutGoods)
    {
        return wmsOutGoodsMapper.updateWmsOutGoods(wmsOutGoods);
    }

    /**
     * 删除【请填写功能名称】对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWmsOutGoodsByIds(String ids)
    {
        return wmsOutGoodsMapper.deleteWmsOutGoodsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param ogid 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteWmsOutGoodsById(Long ogid)
    {
        return wmsOutGoodsMapper.deleteWmsOutGoodsById(ogid);
    }

    /**
     * 批准出库
     * @param onum
     * @return
     */
    @Override
    public int outStore(String onum) {
        //修改出货单状态为已出库
        String ogstatus = "已出库";
        int i = wmsOutGoodsMapper.updateWmsOutGoodsByOgonum(ogstatus,onum);
        //修改订单状态为已发货
        String ostatus = "已发货";
        wmsOrderMapper.updateWmsOrderByOnum(ostatus,onum);
        //获取出货数量
        WmsOrder wmsOrder = wmsOrderMapper.selectWmsOrderByOnum(onum);
        Long opnum = wmsOrder.getOpnum();
        //出货商品名称
        String opname = wmsOrder.getOpname();
        //获取库存数量
        WmsStore wmsStore = wmsStoreMapper.selectWmsStoreBySname(opname);
        Long snum = wmsStore.getSnum();
        Long lastSnum = snum - opnum;
        WmsStore wmsStore1 = new WmsStore();
        wmsStore1.setSid(wmsStore.getSid());
        wmsStore1.setSnum(lastSnum);
        //计算出货后库存的数量
        if (lastSnum<300L){
            wmsStore1.setSstatus("库存不足");
        }else {
            wmsStore1.setSstatus("库存充足");
        }
        //更改库存数量
        wmsStoreMapper.updateWmsStore(wmsStore1);
        return i;
    }
}
